home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The 640 MEG Shareware Studio 2
/
The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO
/
pascal
/
dateunit.zip
/
DATEUNIT.PAS
Wrap
Pascal/Delphi Source File
|
1992-01-02
|
9KB
|
335 lines
(*
#############################################################################
# #
# F R E D D I B B E L Hard- und Softwareentwicklung * 04121 / 92633 #
# Dorfstrasse 132 * W2200 Klein Nordende #
# FRG #
#############################################################################
Copyright Fred Dibbel 1991
This unit can be modified and copied free, as log as this header will stay
with the copy. Usuage is allowed for any NONCOMMERCIAL application. This
means, if you want to use it in one of your programs which is not freeware,
you have to contact me and ask for conditions to use BEFORE selling your
product.
Comments are in german, sorry, but maybe somebody will translate.
*)
{$D+,I-,R-,S-}
unit datum;
{-----------------------------------------------------------------------
enthlt :
function dateok(datum:DateTime):boolean;
berprft Datum auf kalendarische Richtigkeit
function timeok(datum:DateTime):boolean;
berprft Uhrzeit auf formale Richtigkeit
function DateTimeOk(datum):boolean;
beides zusammen
procedure IncDaTi(var basis:DateTime; add:DateTime);
basis wird um DeltaT(add) erhht
Function WeekDay(datum:DateTime):byte;
liefert day-of-week von datum 0=Sonntag .. 6=Samstag
function DaysOfMonth(datum:DateTime):word;
wieviel Tage hat der Monat ??
procedure monday(tweek,tyear:word;var date:DateTime);
liefert Anfangsdatum der Woche
function week(datum:DateTime):word;
liefert Kalenderwoche von Datum, 0 fr letzte Woche Vorjahr
procedure TimeDiffer(a,b:DateTime;var c:TimeDiff);
Zeitdifferenz zwischen a und b
function EqualDT(a,b:DateTime):boolean;
True wenn a=b
function GreaterDT(a,b:DateTime):boolean;
gibt TRUE bei a spter b
function DezHours(datum:TimeDiff):real;
Dezimalequivalent von datum
------------------------------------------------------------------------}
interface
uses dos;
type TimeDiff = record
days : longint;
hours,mins,secs: word;
end;
function dateok(datum:DateTime):boolean;
function timeok(datum:DateTime):boolean;
function DateTimeOk(datum:DateTime):boolean;
procedure IncDaTi(var basis:DateTime; add:DateTime);
Function WeekDay(datum:DateTime):byte;
function DaysOfMonth(datum:DateTime):word;
procedure monday(tweek,tyear:word;var date:DateTime);
procedure TimeDiffer(a,b:DateTime;var c:TimeDiff);
function EqualDT(a,b:DateTime):boolean;
function GreaterDT(a,b:DateTime):boolean;
function DezHours(datum:TimeDiff):real;
function week(datum:DateTime):word;
implementation
function leapyear(year:word):boolean;
begin
if (year mod 4 = 0) and (year mod 100 <> 0) or (year mod 400 = 0)
then leapyear:=true
else leapyear:=false;
end;
function DaysOfMonth(datum:DateTime):word;
begin
with datum do
Case month of 1,3,5,7,8,10,12 : DaysOfMonth:=31;
4,6,9,11 : DaysOfMonth:=30;
2 : if leapyear(year) then DaysOfMonth:=29
else DaysOfMonth:=28
end;
end;
function dateok(datum:DateTime):boolean;
begin
with datum do
dateok:=(month in [1..12]) and (day>0) and (day <=DaysOfMonth(datum));
end;
function timeok(datum:DateTime):boolean;
begin
with datum do
timeok:=(hour in [0..23]) and (min in [0..59]) and (sec in [0..59]);
end;
function DateTimeOk(datum:DateTime):boolean;
begin
DateTimeOk:=dateok(datum) and Timeok(datum);
end;
procedure DTForm(var datum:DateTime);
begin
with datum do
begin
while sec>=60 do begin inc(min); dec(sec,60); end;
while min>=60 do begin inc(hour); dec(min,60); end;
while hour>=24 do begin inc(day); dec(hour,24); end;
while day>DaysOfMonth(datum) do
begin dec(day,DaysOfMonth(datum)); inc(month) end;
while month>12 do begin inc(year); dec(month,12) end;
end;
end;
procedure IncDaTi(var basis:DateTime; add:DateTime);
begin
with basis do
begin
inc(day,add.day);DTForm(basis);
inc(hour,add.hour);DTForm(basis);
inc(min,add.min);DTForm(basis);
inc(sec,add.sec);DTForm(basis);
inc(month,add.month);DTForm(basis);
inc(year,add.year);
end;
end;
function faktor(datum:DateTime):longint;
begin
with datum do
begin
if month in [1,2] then
faktor:=365*year + day + 31*(month - 1) + trunc((year - 1)/4.0) -
trunc(0.75*int(((year - 1)/100.0) + 1))
else faktor:=365*year + day + 31*(month - 1) - trunc(0.4*month + 2.3) +
trunc(year/4.0) - trunc(0.75*int(((year - 1)/100.0) + 1));
end;
end;
Function WeekDay(datum:DateTime):byte;
var fakt : longint;
begin
fakt:=faktor(datum);
fakt:=fakt - 7*trunc(fakt/7.0);
WeekDay:=(fakt + 7) mod 7;
end;
function EqualDT(a,b:DateTime):boolean;
begin
equalDT:=(a.year=b.year) and (a.month=b.month) and (a.day=b.day) and
(a.hour=b.hour) and (a.min=b.min) and (a.sec=b.sec);
end;
function GreaterDT(a,b:DateTime):boolean;
var greater : boolean;
begin
greater:=(a.year>b.year);
if not greater and (a.year=b.year) then
begin
greater:=(a.month>b.month);
if not greater and (a.month=b.month) then
begin
greater:=(a.day>b.day);
if not greater and (a.day=b.day) then
begin
greater:=(a.hour>b.hour);
if not greater and (a.hour=b.hour) then
begin
greater:=(a.min>b.min);
if not greater and (a.min=b.min) then
greater:=(a.sec>b.sec);
end;
end;
end;
end;
GreaterDT:=greater;
end;
procedure TimeDiffer(a,b:DateTime;var c:TimeDiff);
const daysec = 3600 * 24;
var fakta,faktb,daydiff : longint;
seca,secb,secd : longint;
begin
FillChar(c,SizeOf(c),0);
fakta:=faktor(a);faktb:=faktor(b);
seca:=a.sec + 60*a.min + 3600*a.hour;
secb:=b.sec + 60*b.min + 3600*b.hour;
daydiff:=0;
if fakta=faktb then
if seca=secb then exit
else if seca>secb then secd:=seca-secb
else secd:=secb-seca
else if fakta>faktb then
begin
daydiff:=fakta-faktb;
secd:=seca-secb;
end
else begin
daydiff:=faktb-fakta;
secd:=secb-seca;
end;
if secd<0 then
begin
secd:=daysec + secd;
dec(daydiff);
end;
with c do
begin
days:=daydiff;
secs:=secd mod 60;secd:=secd div 60;
mins:=secd mod 60;
hours:=secd div 60;
end;
end;
procedure monday(tweek,tyear:word;var date:DateTime);
var wday,monweek : byte;
plus : DateTime;
hyear : word;
begin
with date do
begin
sec:=0;min:=0;hour:=0;
year:=tyear;day:=1;month:=1;
wday:=WeekDay(date);
if wday>1 then day:=9 - wday else day:=2 - wday;
end; { date = 1. Montag im Jahr }
monweek:=week(date);
if (tweek=0) or ((tweek=1) and (monweek=2)) then
with date do { Woche beginnt im Vorjahr }
begin
dec(year);day:=31;month:=12;
hyear:=year;
monweek:=week(date);
monday(monweek,hyear,date);
end
else begin
if monweek=2 then dec(tweek);
fillchar(plus,sizeof(plus),0);
if tweek>1 then inc(plus.day,7*pred(tweek));
IncDaTi(date,plus);
end;
end;
function week(datum:DateTime):word;
var datum2 : DateTime;
delta : TimeDiff;
wday : byte;
temp : word;
begin
with datum2 do
begin
year:=datum.year;month:=1;day:=1;
hour:=0;min:=0;sec:=0;
end;
TimeDiffer(datum2,datum,delta);
wday:=weekday(datum2);
if wday=0 then wday:=6 else dec(wday);
temp:=((delta.days + wday) div 7);
if wday < 4 then inc(temp);
week:=temp;
end;
function DezHours(datum:TimeDiff):real;
begin
with datum do
dezhours:=24*days + hours + mins/60.0 + secs/3600.0;
end;
begin { }
end.